<?php

namespace App\Admin\Controllers;

use App\Models\AdminUser;
use App\Models\Config;
use Dcat\Admin\Admin;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Traits\HasUploadedFile;
use Illuminate\Http\Request;


class FileController extends AdminController
{
    use HasUploadedFile;

    public function handle(Request $request)
    {
        $disk = $this->disk();

        // 判断是否是删除文件请求
        if ($this->isDeleteRequest()) {
            // 删除文件并响应
            return $this->deleteFileAndResponse();
        }

        // 获取上传的文件
        $file = $this->file();

        // 获取上传的字段名称
        $column = $this->uploader()->upload_column;

        $dir = 'files/'.date("Y/m");
        if ($request->has('dir') && $request->dir) {
            $dir = $dir . '/' . $request->dir;
        }

        if ($request->has('unique_name')) {
            $origin_name = randomkeys(5);
        } else {
            # 保留原文件名
            $origin_name = pathinfo($file->getClientOriginalName(), \PATHINFO_FILENAME);
        }
        $newName = $origin_name . '_' .time().rand(1,1000).'.'.$file->getClientOriginalExtension();

        $result = $this->disk()->putFileAs($dir, $file, $newName);

        $path = "{$dir}/$newName";

        return $result
            ? $this->responseUploaded($path, $disk->url($path))
            : $this->responseErrorMessage('文件上传失败');
    }

    public function downloadTableTpl(Request $request)
    {
        $relation_table = $request->get('table');
        $admin_user = AdminUser::find(Admin::user()->id);
        $dwmc = $admin_user->dwmc;
        if (in_array($relation_table, ['ddb_first', 'ddb_second'])) {
            $dwmc = $admin_user->ddb_dwmc;
        }

        if (in_array($relation_table, ['ddb_users'])) {
            $dwmc = $admin_user->ddb_dwmc;
            $tpl_filename = "中共中国科学技术大学第十三次代表大会代表候选人预备人选登记表.docx";
            $newname = $dwmc."_{$tpl_filename}";
            $file = public_path('tpl/' . $tpl_filename);
            return response()->download($file, $newname);
        }

        if($relation_table == 'ddb_ybrx'){
            $modelClass = isset(Config::$relationClassMaps[$relation_table]) ? Config::$relationClassMaps[$relation_table]: null;
            if ($modelClass) {
                $tpl_filename = $modelClass::NAME . '_'.$admin_user->ddb_dwdm.'.xlsx';
                if (file_exists(public_path('tpl/ddb_ybrx/' . $tpl_filename))) {
                    $newname = $dwmc."_".$tpl_filename;
                    // $file = config('app.url') . '/tpl/' . $tpl_filename;
                    $file = public_path('tpl/ddb_ybrx/' . $tpl_filename);
                    return response()->download($file, $newname);
                }
            }
        }
        $modelClass = isset(Config::$relationClassMaps[$relation_table]) ? Config::$relationClassMaps[$relation_table]: null;
        if ($modelClass) {
            $tpl_filename = $modelClass::NAME . '.xlsx';
            if (file_exists(public_path('tpl/' . $tpl_filename))) {
                $newname = $dwmc."_".$tpl_filename;
                // $file = config('app.url') . '/tpl/' . $tpl_filename;
                $file = public_path('tpl/' . $tpl_filename);
                return response()->download($file, $newname);
            }
        }
        exit('下载失败');
    }

}
